<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@page import="java.sql.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%--
  Created by IntelliJ IDEA.
  User: 123
  Date: 2022/1/4
  Time: 12:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>商品详情</title>
        <link rel="stylesheet" type="text/css" href="css/public.css"/>
        <link rel="stylesheet" type="text/css" href="css/book.css"/>
    </head>
    <body>
        <div class="head">
            <div class="shouye_head">
                <div class="shou_head">
                    <a href="first.jsp">商城首页</a>
                </div>
                <div class="car">
                    <a href="car.jsp"><img src="images/car.png" height="28px">
                        <span>购物车</span>
                    </a>
                </div>
                <div class="login">
                    <div><a href="reg.jsp">登录</a></div>
                    <div><a href="reg.jsp">注册</a></div>
                </div>
            </div>
        </div>
        <div class="xi_nav">
            <div class="wrap">
                <ul class="menu-list">
                    <li class="menu"><a href="first.jsp">首页</a></li>
                </ul>
            </div>
        </div>
        <div class="w">
            <div class="preview" id="preview">
                <div class="photo_xi">
                    <img src="${good.pic}" width="350px">
                </div>
            </div>
            <div class="itemInfo-wrap">
                <div class="x-goodsname">
                    <h3>${good.goods_name}</h3>
                </div>
                <div class="news">
                    <div class="" >
                        ${good.details}
                    </div>
                </div>
                <div class="x-price">
                    <span>￥</span>
                    <div class="x-price_num">${good.unitprice}</div>
                </div>
                <div class="x-action">
                    <!-- product.jsp -->
                    <form action="buyNow" method="post" style="margin-bottom: 10px;"> <!-- 添加了margin-bottom样式 -->
                        <input type="hidden" name="id" value="${good.goods_id}">
                        <button type="submit">立即购买</button>
                    </form>
                    <button type="button" onclick="addToCart('${good.goods_id}')" id="addCartBtn" style="margin-top: 10px;">加入购物车</button>


                    <script>
                        function addToCart(goodsId) {
                            var xhr = new XMLHttpRequest();
                            xhr.open("POST", "AddToCartServlet", true);
                            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                            xhr.onreadystatechange = function() {
                                if (xhr.readyState == 4 && xhr.status == 200) {
                                    var response = xhr.responseText; // 假设服务器返回的是添加结果
                                    if(response === 'success'){
                                        alert("商品已添加到购物车");
                                    } else {
                                        alert("添加失败，请稍后再试");
                                    }
                                }
                            };
                            xhr.send("goods_id=" + goodsId);
                        }
                    </script>

        </div>
    </body>
</html>
